namespace SimpleWebServices.Models
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class TelephoneCollectionFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TelephoneCollection>()
                .DerivesFrom<Collection<Telephone>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("telephones")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            TelephoneCollection actual = (
                "<telephones>" +
                    "<telephone number='02078085900' key='work' />" +
                    "<telephone number='02076301340' key='fax' />" +
                "</telephones>").Deserialize<TelephoneCollection>();

            Assert.NotNull(actual);

            Assert.Equal<string>("02078085900", actual[0].Number);
            Assert.Equal<string>("work", actual[0].Key);
            Assert.Equal<string>("02076301340", actual[1].Number);
            Assert.Equal<string>("fax", actual[1].Key);
        }

        [Fact]
        public void deserializeEmpty()
        {
            TelephoneCollection actual = "<telephones />".Deserialize<TelephoneCollection>();

            Assert.NotNull(actual);
        }

        [Fact]
        public void serialize()
        {
            TelephoneCollection obj = new TelephoneCollection
            {
                new Telephone
                {
                    Number = "02078085900",
                    Key = "work"
                },
                new Telephone
                {
                    Number = "02076301340",
                    Key = "fax"
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("2=count(/telephones/telephone)"));
            Assert.True((bool)navigator.Evaluate("1=count(/telephones/telephone[@key='work'][@number='02078085900'])"));
            Assert.True((bool)navigator.Evaluate("1=count(/telephones/telephone[@key='fax'][@number='02076301340'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TelephoneCollection());
        }

        [Fact]
        public void op_indexer_string()
        {
            TelephoneCollection obj = new TelephoneCollection
            {
                new Telephone
                {
                    Number = "02078085900",
                    Key = "work"
                },
                new Telephone
                {
                    Number = "02076301340",
                    Key = "fax"
                }
            };

            Assert.Same(obj[1], obj["fax"]);
        }

        [Fact]
        public void op_Contains_string()
        {
            TelephoneCollection obj = new TelephoneCollection
            {
                new Telephone
                {
                    Number = "02078085900",
                    Key = "work"
                }
            };

            Assert.True(obj.Contains("work"));
            Assert.False(obj.Contains("fax"));
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new TelephoneCollection() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new TelephoneCollection() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new TelephoneCollection() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}